package com.acce.gxsc.page.supervision.list;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.InCarBean;
import com.acce.gxsc.page.outCar.detail.OutCarDetailActivity;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.widget.AcceBar;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class ToOutCarListActivity extends BaseMvpActivity<ToOutCarListView, ToOutCarListPresenter> implements ToOutCarListView {

    CommonAdapter<InCarBean> mCommonAdapter;
    ArrayList<InCarBean> data;

    @Bind(R.id.rv_goods_prediction)
    RecyclerView mRvGoodsPrediction;
    @Bind(R.id.srl)
    SwipeRefreshLayout mSwipeRefreshLayout;

    String carNo = "";
    String boxNo = "";
    String time = "";
    String hangNo = "";
    String contactName = "";
    String outCarStatus = "";

    int lastVisibleItem = 0;
    @Bind(R.id.tv_count)
    TextView mTvCount;
    @Bind(R.id.accebar)
    AcceBar mAccebar;

    @Override
    protected ToOutCarListPresenter createPresenter() {
        return new ToOutCarListPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_out_car_list_1);
        ButterKnife.bind(this);
        init();

        if (getIntent().hasExtra("carNo")) {
            carNo = getIntent().getStringExtra("carNo");
        }
        if (getIntent().hasExtra("boxNo")) {
            boxNo = getIntent().getStringExtra("boxNo");
        }
        if (getIntent().hasExtra("time")) {
            time = getIntent().getStringExtra("time");
        }
        if (getIntent().hasExtra("carHangNo")) {
            hangNo = getIntent().getStringExtra("carHangNo");
        }
        if (getIntent().hasExtra("contactName")) {
            contactName = getIntent().getStringExtra("contactName");
        }
        if (getIntent().hasExtra("outCarStatus")) {
            outCarStatus = getIntent().getStringExtra("outCarStatus");
        }
        mPresenter.loadInCarList(false, carNo, boxNo, time, hangNo, contactName, outCarStatus);
    }

    private void init() {
        data = new ArrayList<>();
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRvGoodsPrediction.setLayoutManager(linearLayoutManager);
        mRvGoodsPrediction.setAdapter(mCommonAdapter = new CommonAdapter<InCarBean>(this, R.layout.item_out_car, data) {
            @Override
            protected void convert(ViewHolder holder, final InCarBean saleSheet, final int position) {
                if ("1".equals(saleSheet.superviseOther)) {
                    holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.green));
                } else {
                    holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.white));
                }
                holder.setText(R.id.tv_send_company, saleSheet.plateNum);
                holder.setText(R.id.tv_time, saleSheet.createTime.substring(6, 16));
                holder.setText(R.id.tv_car_no, Tools.getString1(saleSheet.containNo, saleSheet.hangNum));
                holder.setText(R.id.tv_box_no, saleSheet.contactName);
                holder.setOnClickListener(R.id.rl_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent outCarDetailIntent = new Intent(ToOutCarListActivity.this, OutCarDetailActivity.class);
                        outCarDetailIntent.putExtra("queryId", saleSheet.inCarId);
                        startActivity(outCarDetailIntent);
                    }
                });
            }
        });

        mSwipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.material_blue, R.color.material_green);
        mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadInCarList(false, carNo, boxNo, time, hangNo, contactName, outCarStatus);
            }
        });

        mRvGoodsPrediction.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (!mSwipeRefreshLayout.isRefreshing() && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mCommonAdapter.getItemCount()) {
                    loadMore();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void loadMore() {
        mPresenter.loadInCarList(true, carNo, boxNo, time, hangNo, contactName, outCarStatus);
    }

    @Override
    public void loadInCarListSuccess(boolean isLoadMore, ArrayList<InCarBean> inCarList, String count) {
        if (!isLoadMore) {
            if (getIntent().hasExtra("outCarStatus")) {
                if ("0".equals(outCarStatus)) {
                    mTvCount.setVisibility(View.VISIBLE);
                    if (inCarList == null) {
                        Tools.setStringWithTitle(mTvCount, "总车次：0车次");
                    } else {
                        Tools.setStringWithTitle(mTvCount, "总车次：", count + "车次");
                    }
                }
            }
            mCommonAdapter.getDatas().clear();
        }
        if (inCarList != null) {
            mCommonAdapter.getDatas().addAll(inCarList);
        }
        mCommonAdapter.notifyDataSetChanged();
    }

    Runnable openRunnable;
    Runnable closeRunnable;

    public void setRefreshing(boolean refreshing) {
        if (refreshing) {
            if (openRunnable == null) {
                openRunnable = new Runnable() {
                    @Override
                    public void run() {
                        if (mSwipeRefreshLayout != null) {
                            mSwipeRefreshLayout.setRefreshing(true);
                        }
                    }
                };
            }
            getWindow().getDecorView().post(openRunnable);
        } else {
            if (closeRunnable == null) {
                closeRunnable = new Runnable() {
                    @Override
                    public void run() {
                        if (mSwipeRefreshLayout != null) {
                            mSwipeRefreshLayout.setRefreshing(false);
                        }
                    }
                };
            }
            getWindow().getDecorView().postDelayed(closeRunnable, 300);
        }
    }
}
